package com.xpgk.beetl.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.beetl.core.Tag;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.xpgk.mvc.baseInfoSet.BaseInfoSet;

/**
 * 参数输出标签：支持select、radio、checkbox
 * 
 */
public class BaseInfoTag extends Tag{
	private static final Log log = Log.getLog(BaseInfoTag.class);

	@SuppressWarnings("unchecked")
	@Override
	public void render() {
		try {
			Map<String, String> param = (Map<String, String>) args[1];

			if(log.isDebugEnabled()) log.debug("获取标签属性");

			String type = param.get("type") == null ? "" : param.get("type");
			
			String id = param.get("id") == null ? "" : param.get("id");
			String name = param.get("name") == null ? "" : param.get("name");
			
			String class_ = param.get("class") == null ? "" : param.get("class");
			String style = param.get("style") == null ? "" : param.get("style");

			String code = param.get("code") == null ? "" : param.get("code");
			String baseinfo=param.get("baseinfo") == null ? "" : param.get("baseinfo");
			String defaultCode = param.get("defaultCode") == null ? "" : param.get("defaultCode");
			String isSelectDef = param.get("isSelectDef") == null ? "true" : param.get("isSelectDef");

			
			if(type.equals("")){
				ctx.byteWriter.writeString(select(id, name, class_, style, code, defaultCode, isSelectDef,baseinfo));
				
			}else if(type.equals("select")){
				ctx.byteWriter.writeString(select(id, name, class_, style, code, defaultCode, isSelectDef,baseinfo));
				
			}else if(type.equals("select_val")){
				ctx.byteWriter.writeString(select_val(id, name, class_, style, code, defaultCode,baseinfo));
			}else if(type.equals("select_2")){
				ctx.byteWriter.writeString(select2(id, name, class_, style, code, defaultCode, isSelectDef,baseinfo));
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下拉标签 value=ids
	 * @param id
	 * @param name
	 * @param class_
	 * @param style
	 * @param number
	 * @param defaultnumber
	 * @return
	 */
	private String select(String id, String name, String class_, String style, String code, String defaultCode, String isSelectDef,String baseinfo){
		StringBuilder sb = new StringBuilder();
		if (StrKit.notBlank(id)) {
			sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
			sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" >");
		} else {
			sb.append("<select name=\"").append(name).append("\" class=\"").append(class_);
			sb.append("\" style=\"").append(style).append("\" >");
		}

//		Code parentCode = Code.cacheGet(Code.getCode(code));
//		List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());
		BaseInfoSet bis=BaseInfoSet.dao.findById(BaseInfoSet.getBaseinfo(baseinfo));
		List<BaseInfoSet>  bisList=bis.getChild();
		sb.append("<option value='-1'>-请选择-</option>");
		for (BaseInfoSet bi : bisList) {
			String status = bi.getStr(BaseInfoSet.column_status);
			String namesTemp = bi.getStr(BaseInfoSet.column_name);
			String isDefault = bi.getStr(BaseInfoSet.column_isDefault);
			String ids = bi.getPKValue();
			if(null == status || status.equals("0")){
				continue;
			}
			if (null != defaultCode && null != ids && defaultCode.equals(ids)) {// 默认选中
				sb.append("<option value=\"").append(ids).append("\" selected=\"selected\">");
				sb.append(namesTemp);
				sb.append("</option>");
			} else {
				if("1".equals(isDefault) && "true".equals(isSelectDef)){
					sb.append("<option value=\"").append(ids).append("\" selected=\"selected\">");
					sb.append(namesTemp);
					sb.append("</option>");
				}else{
					sb.append("<option value=\"").append(ids).append("\">");
					sb.append(namesTemp);
					sb.append("</option>");
				}
			}
		}
		sb.append("</select>");
		
		return sb.toString();
	}
	/**
	 * 下拉备选
	 * @param id
	 * @param name
	 * @param class_
	 * @param style
	 * @param code
	 * @param defaultCode
	 * @param isSelectDef
	 * @param baseinfo
	 * @return
	 */
	private String select2(String id, String name, String class_, String style, String code, String defaultCode, String isSelectDef,String baseinfo){
		StringBuilder sb = new StringBuilder();
		if (StrKit.notBlank(id)) {
			sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
			sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" >");
		} else {
			sb.append("<select name=\"").append(name).append("\" class=\"").append(class_);
			sb.append("\" style=\"").append(style).append("\" >");
		}

//		Code parentCode = Code.cacheGet(Code.getCode(code));
//		List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());
		BaseInfoSet bis=BaseInfoSet.dao.findById(BaseInfoSet.getBaseinfo(baseinfo));
		List<BaseInfoSet>  bisList=bis.getChild();
		sb.append("<option value='-1'>-请选择-</option>");
		for (BaseInfoSet bi : bisList) {
			String status = bi.getStr(BaseInfoSet.column_status);
			String namesTemp = bi.getStr(BaseInfoSet.column_name);
			String isDefault = bi.getStr(BaseInfoSet.column_isDefault);
			String ids = bi.getPKValue();
			if(null == status || status.equals("0")){
				continue;
			}
			if (null != defaultCode && null != ids && defaultCode.equals(ids)) {// 默认选中
				sb.append("<option value=\"").append(namesTemp.trim()).append("\" selected=\"selected\">");
				sb.append(namesTemp);
				sb.append("</option>");
				System.out.println(sb);

			} else {
				if("1".equals(isDefault) && "true".equals(isSelectDef)){
					sb.append("<option value=\"").append(namesTemp.trim()).append("\" selected=\"selected\">");
					sb.append(namesTemp);
					sb.append("</option>");
				}else{
					sb.append("<option value=\"").append(namesTemp.trim()).append("\">");
					sb.append(namesTemp);
					sb.append("</option>");
				}
			}
		}
		sb.append("</select>");
		
		return sb.toString();
	}
	/**
	 * 下拉标签 value=code值
	 * @param id
	 * @param name
	 * @param class_
	 * @param style
	 * @param number
	 * @param defaultnumber
	 * @return
	 */
	private String select_val(String id, String name, String class_, String style, String code, String defaultCode,String baseinfo){
		StringBuilder sb = new StringBuilder();
//		if (StrKit.notBlank(id)) {
//			sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
//			sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" >");
//		} else {
//			sb.append("<select name=\"").append(name).append("\" class=\"").append(class_);
//			sb.append("\" style=\"").append(style).append("\" >");
//		}
//
//		Code parentCode = Code.cacheGet(Code.getCode(code));
//		List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());
//
//		for (Code c : codeList) {
//			String status = c.getStr("status");
//			String namesTemp = c.getStr("name");
//			String dmz = c.getStr("code");
//			String ids = c.getPKValue();
//			if(null == status || status.equals("0")){
//				continue;
//			}
//			if (null != defaultCode && null != ids && defaultCode.equals(namesTemp)) {// 默认选中
//				sb.append("<option value=\"").append(dmz).append("\" selected=\"selected\">");
//				sb.append(namesTemp);
//				sb.append("</option>");
//
//			} else {
//				sb.append("<option value=\"").append(dmz).append("\">");
//				sb.append(namesTemp);
//				sb.append("</option>");
//			}
//		}
//		sb.append("</select>");
		
		return sb.toString();
	}

}
